﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace yichu_front
{
    public partial class FormBOMListPriceHistoryV2 : Form
    {
        private string 修改时间;
        private string 备注;
        public FormBOMListPriceHistoryV2()
        {
            InitializeComponent();
        }
        public FormBOMListPriceHistoryV2(string 修改时间, string 备注)
        {
            this.修改时间 = 修改时间;
            this.备注 = 备注;
            InitializeComponent();
        }

        private void FormBOMListPriceHistoryV2_Load(object sender, EventArgs e)
        {
            getAllBOMType();
        }
        private void getAllBOMType()
        {
            //添加后刷新会重复添加，先清空
            this.tree_BOMType.Nodes.Clear();
            List<BOMType> BOMTypes = SqlSugarHelp.scope.Queryable<BOMType>().ToTree(it => it.Child, it => it.parentId, 0);
            //加载分类
            LoadTreeView(this.tree_BOMType.Nodes, BOMTypes);
        }
        /// <summary>
        /// 加载型号分类TreeView
        /// </summary>
        /// <param name="treeNodeCollection"></param>
        /// <param name="materials"></param>
        private void LoadTreeView(TreeNodeCollection treeNodeCollection, List<BOMType> BOMTypes)
        {
            if (BOMTypes != null && BOMTypes.Count > 0)
            {
                for (int i = 0; i < BOMTypes.Count; i++)
                {
                    TreeNode treeNode = treeNodeCollection.Add(BOMTypes[i].id.ToString(), BOMTypes[i].型号类别, 0, 1);
                    getAllBOMTypeByParentId(treeNode);
                    LoadTreeView(treeNode.Nodes, BOMTypes[i].Child);
                }
            }
        }

        /// <summary>
        /// 根据型号分类TreeNode的id查询具体型号
        /// </summary>
        /// <param name="node"></param>
        private void getAllBOMTypeByParentId(TreeNode node)
        {
            int parentId = Convert.ToInt32(node.Name);
            List<BOMModel> BOMModels = GetBOMModelBybomtypeId(parentId);
            LoadBOMModelTreeView(node.Nodes, BOMModels);
        }

        /// <summary>
        /// 根据Parentid查询所有型号
        /// </summary>
        /// <param name="bomtypeId"></param>
        /// <returns></returns>
        private List<BOMModel> GetBOMModelBybomtypeId(int bomtypeId)
        {
            List<BOMModel> BOMModels = SqlSugarHelp.scope.Queryable<BOMModel>().Where(it => it.bomtypeId == bomtypeId).ToList();
            return BOMModels;
        }
        /// <summary>
        /// 加载具体型号
        /// </summary>
        /// <param name="treeNodeCollection"></param>
        /// <param name="BOMModels"></param>
        private void LoadBOMModelTreeView(TreeNodeCollection treeNodeCollection, List<BOMModel> BOMModels)
        {
            if (BOMModels != null && BOMModels.Count > 0)
            {
                for (int i = 0; i < BOMModels.Count; i++)
                {
                    TreeNode treeNode = treeNodeCollection.Add(BOMModels[i].id.ToString(), BOMModels[i].型号, 2, 2);
                    treeNode.Tag = "BOMModel";
                }
            }
        }

        private void tree_BOMType_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            //鼠标左键 只显示具体材料，不显示材料分类下的材料
            if (e.Button == MouseButtons.Left)
            {
                if (e.Node.Tag != null && e.Node.Tag.ToString() == "BOMModel")
                {

                    //获取点击节点的 Name（数据的ID）
                    int BOMModelid = Convert.ToInt32(e.Node.Name);
                    //查询当前型号ID下的所有规格并组装成datatable
                    getBOMListPriceHistoryByModelId(BOMModelid);

                }
            }
        }
        private void getBOMListPriceHistoryByModelId(int bOMModelid)
        {
            string sql = @"select 序号, [规格],[单价] from [BOMListPriceHistory] where 修改时间 = @修改时间 and 备注 = @备注 and bomModelId = @bomModelId order by 序号";
            DataTable dt = SqlSugarHelp.scope.Ado.GetDataTable(sql, new { 修改时间 = this.修改时间, 备注 = this.备注, bomModelId = bOMModelid });
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt.Rows[i]["序号"] = i + 1;
                }
            }
            this.grid_BOMList1.DataSource = dt;
            grid_BOMList1.Refresh();
        }

    }
}
